package cn.neusoft.servlet;

import cn.neusoft.utils.IOUtils;
import cn.neusoft.vo.ResultBean;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonStr = IOUtils.readStr(req.getInputStream());

        JSONObject user = (JSONObject) JSONObject.parse(jsonStr);
        String username = user.get("username").toString();
        String password = user.get("password").toString();

        ResultBean resultBean = ResultBean.ERROR("登录失败,用户名或密码错误");

        if ("zhizihua".equals(username) && "123456".equals(password)){
            //登录成功  将登录成功的用户信息存入session
            HttpSession session = req.getSession();
            System.out.println("登录成功："+session.getId());
            session.setAttribute("adminUser",user);
            resultBean = ResultBean.SUCCESS("admin");
        }

        resp.getWriter().write(JSONObject.toJSONString(resultBean));

    }
}
